{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "f04dd603-a2d1-48ce-8c17-9f1dba8de1ee",
   "metadata": {},
   "source": [
    "Chapter 01\n",
    "\n",
    "# 球坐标、三维直角坐标\n",
    "《线性代数》 | 鸢尾花书：数学不难"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ccafb456-2453-4c82-8a65-b1963a370cb2",
   "metadata": {},
   "source": [
    "这段代码的核心目标是 **在三维空间中实现直角坐标系与球坐标系的相互转换**。在数学上，三维直角坐标 $(x_1, x_2, x_3)$ 可以转换为球坐标 $(r, \\theta, \\phi)$，然后再转换回直角坐标。具体分析如下：\n",
    "\n",
    "---\n",
    "\n",
    "### 1. 直角坐标 $(x_1, x_2, x_3)$ 转换为球坐标 $(r, \\theta, \\phi)$\n",
    "\n",
    "在球坐标系中：\n",
    "- **极径 $r$（radius）** 表示点 $(x_1, x_2, x_3)$ 到原点的距离。\n",
    "- **极角 $\\theta$（inclination angle, polar angle）** 是 $r$ 与 $z$ 轴正方向之间的夹角，取值范围 $0 \\leq \\theta \\leq \\pi$。\n",
    "- **方位角 $\\phi$（azimuthal angle）** 是投影到 $x$-$y$ 平面后的向量与 $x$ 轴正方向的夹角，取值范围 $-\\pi < \\phi \\leq \\pi$。\n",
    "\n",
    "#### (a) 计算极径 $r$\n",
    "\n",
    "极径（L2 范数）定义如下：\n",
    "\n",
    "$$\n",
    "r = \\sqrt{x_1^2 + x_2^2 + x_3^2}\n",
    "$$\n",
    "\n",
    "对于代码中的点 $(2,3,6)$，计算如下：\n",
    "\n",
    "$$\n",
    "r = \\sqrt{2^2 + 3^2 + 6^2} = \\sqrt{4 + 9 + 36} = \\sqrt{49} = 7\n",
    "$$\n",
    "\n",
    "代码 `np.sqrt(x1**2 + x2**2 + x3**2)` 计算了这个结果，并存储在变量 `r` 中。\n",
    "\n",
    "---\n",
    "\n",
    "#### (b) 计算极角 $\\theta$\n",
    "\n",
    "极角 $\\theta$ 由公式计算：\n",
    "\n",
    "$$\n",
    "\\theta = \\arccos \\left(\\frac{x_3}{r} \\right)\n",
    "$$\n",
    "\n",
    "代入计算：\n",
    "\n",
    "$$\n",
    "\\theta = \\arccos \\left(\\frac{6}{7} \\right) \\approx 0.55 \\text{ radians}\n",
    "$$\n",
    "\n",
    "在代码中，为了避免除零错误，使用了 `if r != 0 else 0` 来处理特殊情况 $r = 0$ 时的 $\\theta$。\n",
    "\n",
    "然后，代码 `np.rad2deg(theta)` 将弧度制转换为角度制：\n",
    "\n",
    "$$\n",
    "\\theta_{\\text{deg}} = \\frac{180}{\\pi} \\cdot 0.55 \\approx 32.31^\\circ\n",
    "$$\n",
    "\n",
    "---\n",
    "\n",
    "#### (c) 计算方位角 $\\phi$\n",
    "\n",
    "方位角 $\\phi$ 定义如下：\n",
    "\n",
    "$$\n",
    "\\phi = \\arctan2(x_2, x_1)\n",
    "$$\n",
    "\n",
    "代入计算：\n",
    "\n",
    "$$\n",
    "\\phi = \\arctan2(3, 2) \\approx 0.98 \\text{ radians}\n",
    "$$\n",
    "\n",
    "然后，代码 `np.rad2deg(phi)` 将其转换为角度制：\n",
    "\n",
    "$$\n",
    "\\phi_{\\text{deg}} = \\frac{180}{\\pi} \\cdot 0.98 \\approx 56.31^\\circ\n",
    "$$\n",
    "\n",
    "所以，直角坐标点 $(2,3,6)$ 在球坐标系中的表示为：\n",
    "\n",
    "$$\n",
    "(r, \\theta, \\phi) = (7, 32.31^\\circ, 56.31^\\circ)\n",
    "$$\n",
    "\n",
    "---\n",
    "\n",
    "### 2. 球坐标 $(r, \\theta, \\phi)$ 转换回直角坐标 $(x_1, x_2, x_3)$\n",
    "\n",
    "我们用以下公式将球坐标转换回直角坐标：\n",
    "\n",
    "$$\n",
    "x_1 = r \\sin\\theta \\cos\\phi\n",
    "$$\n",
    "\n",
    "$$\n",
    "x_2 = r \\sin\\theta \\sin\\phi\n",
    "$$\n",
    "\n",
    "$$\n",
    "x_3 = r \\cos\\theta\n",
    "$$\n",
    "\n",
    "代入计算：\n",
    "\n",
    "$$\n",
    "x_1 = 7 \\sin(0.55) \\cos(0.98) \\approx 2\n",
    "$$\n",
    "\n",
    "$$\n",
    "x_2 = 7 \\sin(0.55) \\sin(0.98) \\approx 3\n",
    "$$\n",
    "\n",
    "$$\n",
    "x_3 = 7 \\cos(0.55) \\approx 6\n",
    "$$\n",
    "\n",
    "结果 $(x_1, x_2, x_3) = (2,3,6)$ 与原始坐标一致，验证了坐标转换的正确性。\n",
    "\n",
    "---\n",
    "\n",
    "### 结论\n",
    "\n",
    "1. **直角坐标 $(x_1, x_2, x_3)$ 转换为球坐标 $(r, \\theta, \\phi)$**：\n",
    "   - 计算极径 $r = \\sqrt{x_1^2 + x_2^2 + x_3^2}$。\n",
    "   - 计算极角 $\\theta = \\arccos(x_3 / r)$。\n",
    "   - 计算方位角 $\\phi = \\arctan2(x_2, x_1)$。\n",
    "\n",
    "2. **球坐标 $(r, \\theta, \\phi)$ 转换回直角坐标 $(x_1, x_2, x_3)$**：\n",
    "   - 计算 $x_1 = r \\sin\\theta \\cos\\phi$。\n",
    "   - 计算 $x_2 = r \\sin\\theta \\sin\\phi$。\n",
    "   - 计算 $x_3 = r \\cos\\theta$。\n",
    "\n",
    "3. **转换验证**：\n",
    "   - 计算的直角坐标 $(x_1, x_2, x_3) = (2,3,6)$ 与原始输入一致，说明计算正确。\n",
    "   - 该转换在 **3D 计算机图形学、地球物理、天文学、机器人学** 等领域广泛应用，例如球面投影、摄像机视角计算、地球经纬度转换等。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "53a7779f-af60-4c98-83ad-70fa48d40be9",
   "metadata": {},
   "source": [
    "## 初始化"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "29bebadb-8243-4041-a3c5-196b4bb25798",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e3d3dcfb-d8ef-45a9-a279-0c0bda21a9a0",
   "metadata": {},
   "source": [
    "## 三维直角坐标转化为球坐标"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "28a0e52a-24bc-49e1-ba05-df2f529633a5",
   "metadata": {},
   "outputs": [],
   "source": [
    "x1, x2, x3 = 2, 3, 6  # 直角坐标 (2, 3, 6)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "bea7a0b5-6520-4c60-915f-a9607dd082f8",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "7.0"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r = np.sqrt(x1**2 + x2**2 + x3**2)  # 计算极径 r\n",
    "r"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "a1416e3a-de0b-4c67-8248-4da24f87b4d7",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.5410995259571458"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "theta = np.arccos(x3 / r) if r != 0 else 0  # 计算极角\n",
    "theta"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "54b975b6-b6d3-4ff6-9d9b-a1e8700761da",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "31.002719133873992"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.rad2deg(theta)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "ebee07e3-572c-4c5b-9c7c-a5f99c9a1e99",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.982793723247329"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "phi = np.arctan2(x2, x1)  # 计算方位角\n",
    "phi"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "ff7ab4ab-4348-411d-beaa-97954ab78df0",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "56.309932474020215"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.rad2deg(phi)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "df4570ff-5532-44a9-afd7-ec13bf3af603",
   "metadata": {},
   "source": [
    "## 球坐标转化为三维直角坐标"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "2633a2e2-4284-4649-83f8-37b4d884d9b8",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2.0000000000000004"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x1_ = r * np.sin(theta) * np.cos(phi)\n",
    "x1_"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "c4b11f6b-5372-427f-b546-130bddf2ed56",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3.0"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x2_ = r * np.sin(theta) * np.sin(phi)\n",
    "x2_"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "c3f6d7d1-0f06-4116-9298-bf094d2a743c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "6.0"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x3_ = r * np.cos(theta)\n",
    "x3_"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "177f34ba-360d-4db0-b5c8-a15a53bf52f1",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "7035b11c-ae7a-41c4-9594-9ff48182ccc4",
   "metadata": {},
   "source": [
    "作者\t**生姜DrGinger**  \n",
    "脚本\t**生姜DrGinger**  \n",
    "视频\t**崔崔CuiCui**  \n",
    "开源资源\t[**GitHub**](https://github.com/Visualize-ML)  \n",
    "平台\t[**油管**](https://www.youtube.com/@DrGinger_Jiang)\t\t\n",
    "\t\t[**iris小课堂**](https://space.bilibili.com/3546865719052873)\t\t\n",
    "\t\t[**生姜DrGinger**](https://space.bilibili.com/513194466)  "
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python [conda env:base] *",
   "language": "python",
   "name": "conda-base-py"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
